home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / bbs / pad321.zip / TIME.MH < prev    next >
Text File  |  1996-08-21  |  3KB  |  125 lines

  1. // This file defines various time and date manipulation functions.
  2. //
  3. // Function list:
  4. //
  5. // string       timeToString    (struct _time: tme);
  6. // void         strToTime       (string: theString, Ref struct _time: result);
  7. //
  8. // void         timeInit        ();
  9. // bool         timeToken       (string: token, string: params);
  10. //
  11. //
  12.  
  13. #ifndef __TIME_MH
  14. #define __TIME_MH
  15.  
  16. #ifndef __GENERAL_MH
  17. #include "general.mh"
  18. #endif
  19.  
  20. void setTime (Ref struct _time: dest, struct _time: source) {
  21.   dest.hh := source.hh;
  22.   dest.mm := source.mm;
  23.   dest.ss := source.ss;
  24.   }
  25.  
  26. void getTime (Ref struct _time: curTime) {
  27.   struct _stamp: curDate;
  28.   long: intDate;
  29.  
  30.   intDate := time ();
  31.   long_to_stamp (intDate, curDate);
  32.   setTime (curTime, curDate.time);
  33.   }
  34.  
  35. // betweenTimes determines if testTime is within the range defined by startTime and
  36. // endTime.
  37.  
  38. bool betweenTimes (struct _time: startTime, struct _time: endTime, struct _time: testTime) {
  39.   struct _stamp: s, e, t;
  40.   long: intStart, intTime, intEnd;
  41.  
  42.   setTime (s.time, startTime);
  43.   setTime (e.time, endTime);
  44.   setTime (t.time, testTime);
  45.   intTime := stamp_to_long (t);
  46.   intStart := stamp_to_long (s);
  47.   intEnd   := stamp_to_long (e);
  48.  
  49.   if (intStart > intEnd) {
  50.     if ((intTime < intStart) and (intTime > intEnd)) return False;
  51.     }
  52.   else if (intStart < intEnd) {
  53.     if ((intTime < intStart) or (intTime > intEnd)) return False;
  54.     };
  55.  
  56.   return True;
  57.   }
  58.  
  59. // Converts a time structure into a printable string.
  60. //
  61. // Format of the string is:
  62. //
  63. // hh:mm.ss
  64. //
  65. // The .ss portion is skipped if ss = 0.
  66. //
  67. // This will probably be rewritten to use the time format defined in MAX.CTL
  68. // in the future.
  69.  
  70. string timeToString (struct _time: tme) {
  71.   string: result;
  72.   string: temp;
  73.   result := itostr (tme.hh);
  74.   if (strlen (result) < 2) result := "0" + result;
  75.   if (tme.mm < 10) {
  76.     temp := "0" + itostr (tme.mm);
  77.     }
  78.   else temp := itostr (tme.mm);
  79.   result := result + ":" + temp;
  80.   if (tme.ss) result := result + "." + itostr (tme.ss);
  81.   return result;
  82.   }
  83.  
  84. // strToTime converts a string containing a time into a _time struct.
  85. // The format of the input time is:
  86. //
  87. // hh:mm.ss
  88. //
  89. // The mm and ss fields are optional, and if not included, are set
  90. // to 0 by default.
  91.  
  92. void strToTime (string: theString, Ref struct _time: result) {
  93.   int:
  94.     hour, min, sec,
  95.     len, hourend, minend;
  96.  
  97.   len := strlen (theString);
  98.  
  99.   hourend := stridx (theString, 1, ':');
  100.   if (hourend = 0) {
  101.     hour := strtoi (theString);
  102.     min  := 0;
  103.     sec  := 0;
  104.     }
  105.   else {
  106.     hour := strtoi (substr (theString, 1, len - hourend + 1));
  107.     minend := stridx (theString, hourend+1, ':');
  108.     if (minend = 0) minend := stridx (theString, hourend+1, '.');
  109.     if (minend = 0) {
  110.        min  := strtoi (substr (theString, hourend+1, len - hourend));
  111.        sec  := 0;
  112.        }
  113.     else {
  114.        min  := strtoi (substr (theString, hourend+1, minend - hourend));
  115.        sec  := strtoi (substr (theString, minend+1, len - minend));
  116.        };
  117.     };
  118.   result.hh := hour;
  119.   result.mm := min;
  120.   result.ss := sec;
  121.   }
  122.  
  123. #endif
  124.  
  125.